package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Contact;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ContactDao {
	DBHelp<Contact> db = new DBHelp<Contact>();

	public List<Contact> findAllByProjectId(String projectId) {
		final String sql = "SELECT id,`name`,companyname,mobile,tel,email,address,website,weibo,content,createtime,projectid FROM t_contact WHERE projectid=?";
		return db.executeQueryForList(sql, new ContactRowMapper(), projectId);
	}
	
	private class ContactRowMapper implements RowMapper<Contact> {

		public Contact mapRow(ResultSet rs) throws SQLException {
			Contact c = new Contact();
			c.setAddress(rs.getString("address"));
			c.setCompanyname(rs.getString("companyname"));
			c.setContent(rs.getString("content"));
			c.setCreatetime(rs.getString("createtime"));
			c.setEmail(rs.getString("email"));
			c.setId(rs.getString("id"));
			c.setMobile(rs.getString("mobile"));
			c.setName(rs.getString("name"));
			c.setTel(rs.getString("tel"));
			c.setWebsite(rs.getString("website"));
			c.setWeibo(rs.getString("weibo"));
			c.setProjectid("projectid");
			return c;
		}
		
	}

	public void add(Contact c) {
		final String sql = "INSERT INTO t_contact(id,`name`,companyname,mobile,tel,email,address,website,weibo,content,createtime,projectid) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, c.getId(),c.getName(),c.getCompanyname(),c.getMobile(),c.getTel(),c.getEmail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getContent(),c.getCreatetime(),c.getProjectid());
	}

	public Contact findById(String id) {
		final String sql = "SELECT id,`name`,companyname,mobile,tel,email,address,website,weibo,content,createtime,projectid FROM t_contact WHERE id = ?";
		return db.executeQueryForObject(sql, new ContactRowMapper(), id);
	}

	public void edit(Contact c) {
		final String sql = "update t_contact set `name`=?,companyname=?,mobile=?,tel=?,email=?,address=?,website=?,weibo=?,content=?,projectid=? where id=? ";
		db.executeSQL(sql, c.getName(),c.getCompanyname(),c.getMobile(),c.getTel(),c.getEmail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getContent(),c.getProjectid(),c.getId());
	}

	public void del(String id) {
		final String sql = "DELETE FROM t_contactnote WHERE contactid=?";
		db.executeSQL(sql, id);
		final String sql2 = "DELETE FROM t_contact WHERE id=?";
		db.executeSQL(sql2, id);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
